# coding:utf-8

"""

host
    cpu


guest
    


"""

testparse = """
<capabilities>

  <host>
    <uuid>44454c4c-5700-104c-804c-cac04f443358</uuid>
    <cpu>
      <arch>x86_64</arch>
      <model>Westmere</model>
      <vendor>Intel</vendor>
      <topology sockets='1' cores='4' threads='1'/>
      <feature name='rdtscp'/>
      <feature name='x2apic'/>
      <feature name='xtpr'/>
      <feature name='tm2'/>
      <feature name='est'/>
      <feature name='vmx'/>
      <feature name='ds_cpl'/>
      <feature name='monitor'/>
      <feature name='pbe'/>
      <feature name='tm'/>
      <feature name='ht'/>
      <feature name='ss'/>
      <feature name='acpi'/>
      <feature name='ds'/>
      <feature name='vme'/>
    </cpu>
    <power_management>
      <suspend_mem/>
      <suspend_disk/>
      <suspend_hybrid/>
    </power_management>
    <migration_features>
      <live/>
      <uri_transports>
        <uri_transport>tcp</uri_transport>
      </uri_transports>
    </migration_features>
    <topology>
      <cells num='1'>
        <cell id='0'>
          <cpus num='4'>
            <cpu id='0'/>
            <cpu id='1'/>
            <cpu id='2'/>
            <cpu id='3'/>
          </cpus>
        </cell>
      </cells>
    </topology>
    <secmodel>
      <model>apparmor</model>
      <doi>0</doi>
    </secmodel>
  </host>

  <guest>
    <os_type>hvm</os_type>
    <arch name='i686'>
      <wordsize>32</wordsize>
      <emulator>/usr/bin/qemu-system-x86_64</emulator>
      <machine>pc-1.0</machine>
      <machine canonical='pc-1.0'>pc</machine>
      <machine>pc-0.14</machine>
      <machine>pc-0.13</machine>
      <machine>pc-0.12</machine>
      <machine>pc-0.11</machine>
      <machine>pc-0.10</machine>
      <machine>isapc</machine>
      <domain type='qemu'>
      </domain>
      <domain type='kvm'>
        <emulator>/usr/bin/kvm</emulator>
        <machine>pc-1.0</machine>
        <machine canonical='pc-1.0'>pc</machine>
        <machine>pc-0.14</machine>
        <machine>pc-0.13</machine>
        <machine>pc-0.12</machine>
        <machine>pc-0.11</machine>
        <machine>pc-0.10</machine>
        <machine>isapc</machine>
      </domain>
    </arch>
    <features>
      <cpuselection/>
      <deviceboot/>
      <pae/>
      <nonpae/>
      <acpi default='on' toggle='yes'/>
      <apic default='on' toggle='no'/>
    </features>
  </guest>

  <guest>
    <os_type>hvm</os_type>
    <arch name='x86_64'>
      <wordsize>64</wordsize>
      <emulator>/usr/bin/qemu-system-x86_64</emulator>
      <machine>pc-1.0</machine>
      <machine canonical='pc-1.0'>pc</machine>
      <machine>pc-0.14</machine>
      <machine>pc-0.13</machine>
      <machine>pc-0.12</machine>
      <machine>pc-0.11</machine>
      <machine>pc-0.10</machine>
      <machine>isapc</machine>
      <domain type='qemu'>
      </domain>
      <domain type='kvm'>
        <emulator>/usr/bin/kvm</emulator>
        <machine>pc-1.0</machine>
        <machine canonical='pc-1.0'>pc</machine>
        <machine>pc-0.14</machine>
        <machine>pc-0.13</machine>
        <machine>pc-0.12</machine>
        <machine>pc-0.11</machine>
        <machine>pc-0.10</machine>
        <machine>isapc</machine>
      </domain>      
    </arch>
    <features>
      <cpuselection/>
      <deviceboot/>
      <acpi default='on' toggle='yes'/>
      <apic default='on' toggle='no'/>
    </features>
  </guest>

</capabilities>
"""

def Parse(xml):
    
    return Capabilities(xml);

    pass

from xml.dom import minidom;

class Capabilities:
    
    def __init__(self, xml):
        
        self.host = None;
        self.guest = [];
        self.current = None;
        
        self.Parse(xml);
        
        for h in self.guest:
            if h.arch.name == self.host.CPU.arch:
                self.current = h.arch.domain[:];
                break;
        
    
    def Parse(self, xml):
        
        dom = minidom.parseString(xml);
        
        root = dom.documentElement;
        
        nodes = root.childNodes;
        
        for node in nodes:
            if(node.nodeName == 'host'):
                self.host = Host();                
                for node1 in node.childNodes:
                    if(node1.nodeName == 'uuid'):
                        self.host.uuid = node1.firstChild.data.strip();
                    elif(node1.nodeName == 'cpu'):
                        self.host.CPU = CPU();
                        for node2 in node1.childNodes:
                            if(node2.nodeName == 'arch'):
                                self.host.CPU.arch = node2.firstChild.data.strip();
                            elif(node2.nodeName == 'model'):
                                self.host.CPU.model = node2.firstChild.data.strip();
                            elif(node2.nodeName == 'vendor'):
                                self.host.CPU.vendor = node2.firstChild.data.strip();
                            elif(node2.nodeName == 'topology'):
                                self.host.CPU.topology["sockets"] = node2.getAttribute('sockets');
                                self.host.CPU.topology['cores'] = node2.getAttribute('cores');
                                self.host.CPU.topology['threads'] = node2.getAttribute('threads');
                            elif(node2.nodeName == 'feature'):
                                self.host.CPU.feature.append(node2.getAttribute('name'));
                
                pass
            elif(node.nodeName == 'guest'):
                g = Guest();                
                self.guest.append(g);                
                for node1 in node.childNodes:
                    if(node1.nodeName == 'os_type'):
                        g.os_type = node1.firstChild.data.strip();
                    elif(node1.nodeName == 'arch'):
                        g.arch = GuestArch();
                        g.arch.name = node1.getAttribute('name');
                        
                        for node2 in node1.childNodes:
                            
                            if(node2.nodeName == 'wordsize'):
                                g.arch.wordsize = node2.firstChild.data.strip();
                            elif(node2.nodeName == 'emulator'):
                                g.arch.emulator = node2.firstChild.data.strip();
                            elif(node2.nodeName == 'domain'):                                
                                d = Domain();
                                g.arch.domain.append(d);
                                d.type = node2.getAttribute("type");
                                for node3 in node2.childNodes:
                                    if(node3.nodeName == 'emulator'):
                                        d.emulator = node3.firstChild.data;
                                    elif(node3.nodeName == 'machine'):
                                        d.machine.append(node3.firstChild.data);
                pass
        
        pass



class CPU:
    def __init__(self):
        
        self.arch = None;
        self.model = None;
        self.vendor = None;
        self.topology = {};
        self.feature = [];
                
        pass

class Domain:
    
    def __init__(self):
        
        self.type = None;
        self.emulator = None;
        self.machine = [];
            
        pass
    
    pass

class GuestArch:
    
    def __init__(self):
        
        self.name = None;
        self.wordsize = None;
        self.emulator = None;
        self.machine = [];
        self.domain = [];
        
        pass

class Host:
   
    def __init__(self):
        
        self.uuid = None;
        self.CPU = None;
         
        pass

class Guest:
   
    def __init__(self):
        self.os_type = None;
        self.arch = None;
        pass
    
if(__name__ == "__main__"):
    c = Parse(testparse);
    print c; 
